package com.liujie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liujie.common.CustomException;
import com.liujie.common.R;
import com.liujie.dto.SetmealDto;
import com.liujie.entity.Setmeal;
import com.liujie.entity.SetmealDish;
import com.liujie.service.CategoryService;
import com.liujie.service.SetmealDishService;
import com.liujie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null, Setmeal::getName,name);
        setmealService.page(pageInfo, lqw);

        Page<SetmealDto> setmealDtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<SetmealDto> setmealDtos=new ArrayList<>();
        List<Setmeal> records = pageInfo.getRecords();
        for (Setmeal record : records) {
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            setmealDto.setSetmealDishes(setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, record.getId())));
            setmealDto.setCategoryName(categoryService.getById(record.getCategoryId()).getName());
            setmealDtos.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

    @CacheEvict(value = "setMealCache",allEntries = true)//删除所有名为setMealCache的缓存
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        boolean b = setmealService.saveWithDish(setmealDto);
        if (b){
            return R.success("新添套餐成功");
        }
        return R.error("新添套餐失败");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getOne(@PathVariable long id){
        SetmealDto setmealDto = setmealService.getOneWithDish(id);
        if (setmealDto!=null){
            return R.success(setmealDto);
        }
        return R.error("数据出错");
    }

    @CacheEvict(value = "setMealCache",allEntries = true)//删除所有名为setMealCache的缓存
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        boolean b = setmealService.updateWithDish(setmealDto);
        if (b){
            return R.success("修改套餐完成");
        }
        return R.error("修改套餐失效");
    }

    @CacheEvict(value = "setMealCache",allEntries = true)//删除所有名为setMealCache的缓存
    @DeleteMapping
    public R<String> delete(long[] ids){
        boolean b = setmealService.deleteWithDish(ids);
        if (b){
            return R.success("删除套餐成功");
        }
        return R.error("正在售卖中的套餐‘删除失败’");
    }

    @PostMapping("/status/0")
    public R<String> updateStatus0(long[] ids){
        boolean b=false;
        for (long id : ids) {
            Setmeal setmeal=new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(0);
            b = setmealService.updateById(setmeal);
        }
        if (b){
            return R.success("套餐停售完成");
        }
        return R.error("套餐停售失败");
    }

    @PostMapping("/status/1")
    public R<String> updateStatus1(long[] ids){
        boolean b=false;
        for (long id : ids) {
            Setmeal setmeal=new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(1);
            b = setmealService.updateById(setmeal);
        }
        if (b){
            return R.success("套餐起售完成");
        }
        return R.error("套餐起售失败");
    }

    @Cacheable(value = "setMealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")//设置缓存
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId())
                .eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus())
                .orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(lqw);
        if (setmeals.size()>0){
            return R.success(setmeals);
        }
        return R.error("无数据");
    }
}
